// search模块的小仓库
// state：仓库存储数据的地方
import {reqGetGoodsInfo,reqAddOrUpdataeShopCart} from '@/api/index'
import {getUUID} from '@/utils/uuid_token'
const state = {
    goodInfo: {},
    // 游客的临时身份
    uuid_token: getUUID()
}
// mutations: 修改state的唯一手段
const mutations = {
    GETGOODINFO(state, goodInfo) {
        state.goodInfo = goodInfo
    }
}

// action: 书写业务逻辑，也可以处理异步
const actions={
    // 获取产品信息
    async getGoodInfo({commit}, skuId) {
        let res = await reqGetGoodsInfo(skuId)
        if(res.code == 200) {
            commit("GETGOODINFO", res.data)
        }
    },
    // 将产品添加到购物车中/修改某一个产品的个数
    async addOrUpdateShopCart({commit}, {skuId, skuNum}) {
        let res = await reqAddOrUpdataeShopCart(skuId, skuNum)
        if(res.code == 200) {
            return "ok"
        } else {
            return Promise.reject(new Error('faile'))
        }
    }
}

// getters: 可以理解为计算属性，用于简化仓库数据，让组件获取仓库的数据更加方便
const getters={
    categoryView(state) {
        return state.goodInfo.categoryView || {}
    },
    skuInfo(state) {
        return state.goodInfo.skuInfo || {}
    },
    skuSaleAttrValueList(state) {
        return state.goodInfo.skuInfo.skuSaleAttrValueList || []
    },
}

export default {
    state,
    mutations,
    actions,
    getters
}
